package tk.djcrazy.tuguaconverter;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class Main {

	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
		// TODO Auto-generated method stub
		System.out.println("请输入图卦地址：");
 		String url;
		String htmlContent;
		String outPutCntent;
		String regex; 
		String PostContent;
		Matcher matcher;
/**		
		try 
		{ 
			br = new BufferedReader(new InputStreamReader(System.in)); 
			url = br.readLine(); 
		}
		catch (Exception e) {
			// TODO: handle exception
			System.out.println("读取地址失败！");
			return;
		}
		*/
		regex="more.asp\\?name=xilei&id=\\d{1,10}(?=\\ title='【喷嚏图卦20110819】)";
		Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		while (true) {
			matcher = pattern.matcher(HttpGetHtml.getHtml(
			"http://www.dapenti.com/blog/index.asp"));
			if (matcher.find()) {
				url = "http://www.dapenti.com/blog/"+matcher.group(); 
				break;
			} 
			System.out.println("未检测到今日图卦，睡一会儿~");
			Thread.sleep(60000);
		}
		
		
		htmlContent = HttpGetHtml.getHtml(url);
 		/**解析标题*/
		outPutCntent = getTitle(htmlContent) +"\n\n";
		/**截取内容段html，去掉多余的*/
		htmlContent = getEntityContent(htmlContent);
		
		/** 请在这里填入需要上传98图片的帐号！*/
 		HttpGetHtml.LoginCC98("DJ", "×××××××××"); 
 		
		regex = "(?<=<P>).*?(?=</P>)|(?<=<P class=source type=\"\\d{0,2}\" mid=\"\\d{1,20}\">).*?(?=</P>)";
		pattern = Pattern.compile(regex, Pattern.DOTALL);
		matcher = pattern.matcher(htmlContent);
 
		while (matcher.find()) {
 			PostContent = matcher.group();
 			if (PostContent.contains("IMG")) {
				HttpGetHtml.DownloadPicture(getPictureUrl(PostContent));
				outPutCntent=outPutCntent +"[upload=jpg]"+ HttpGetHtml.UploadPictureToCC98()+"[/upload]"+"\n\n";
 			}
 			else
 			{
 				PostContent=PostContent.replaceAll("<.*?>", "");
 				outPutCntent=outPutCntent+PostContent+"\n\n";
 			}
 		} 
		
		System.out.println("请复制以下内容：");
		System.out.println(outPutCntent);
		return;
		
	}
	/**
	 * 
	 * @param html
	 * @return
	 */
	public static String getTitle(String html) {
		String regex = "(?<=<title>铂程斋--).*?(?=</title>)";
		Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		Matcher matcher=pattern.matcher(html);
		if (matcher.find()) {
			return matcher.group();
		}
		else {
			return null;
		}  	
	}
	/**
	 * 
	 * @param html
	 * @return
	 */
	private static String getPictureUrl(String html) {
		String ret="";
		String regex=
			"(?<=src=\").*?(?=\">)";
		Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		Matcher matcher=pattern.matcher(html);
		if (matcher.find()) {
			ret = matcher.group();
			return ret;
		}
		else {
			return null;
		}  
	}
	/**
	 * 
	 * @param html
	 * @return
	 */
	private static String getEntityContent(String html) {
		
		String ret="";
		String regex=
			"(?<=</DIV></SPAN><BR><SPAN class=oblog_text>).*?(?=<P>广告联系：dapenti#dapenti.com.*?</P>)";
		Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		Matcher matcher=pattern.matcher(html);
		if (matcher.find()) {
			ret = matcher.group();
			return ret;
		}
		else {
			return null;
		}  
	}
}



















